{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Count Items Matching a Rule"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countMatches"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #统计匹配检索规则的物品数量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个数组 <code>items</code> ，其中 <code>items[i] = [type<sub>i</sub>, color<sub>i</sub>, name<sub>i</sub>]</code> ，描述第 <code>i</code> 件物品的类型、颜色以及名称。</p>\n",
    "\n",
    "<p>另给你一条由两个字符串 <code>ruleKey</code> 和 <code>ruleValue</code> 表示的检索规则。</p>\n",
    "\n",
    "<p>如果第 <code>i</code> 件物品能满足下述条件之一，则认为该物品与给定的检索规则 <strong>匹配</strong> ：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>ruleKey == \"type\"</code> 且 <code>ruleValue == type<sub>i</sub></code> 。</li>\n",
    "\t<li><code>ruleKey == \"color\"</code> 且 <code>ruleValue == color<sub>i</sub></code> 。</li>\n",
    "\t<li><code>ruleKey == \"name\"</code> 且 <code>ruleValue == name<sub>i</sub></code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>统计并返回 <strong>匹配检索规则的物品数量</strong> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>items = [[\"phone\",\"blue\",\"pixel\"],[\"computer\",\"silver\",\"lenovo\"],[\"phone\",\"gold\",\"iphone\"]], ruleKey = \"color\", ruleValue = \"silver\"\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>只有一件物品匹配检索规则，这件物品是 [\"computer\",\"silver\",\"lenovo\"] 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>items = [[\"phone\",\"blue\",\"pixel\"],[\"computer\",\"silver\",\"phone\"],[\"phone\",\"gold\",\"iphone\"]], ruleKey = \"type\", ruleValue = \"phone\"\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>只有两件物品匹配检索规则，这两件物品分别是 [\"phone\",\"blue\",\"pixel\"] 和 [\"phone\",\"gold\",\"iphone\"] 。注意，[\"computer\",\"silver\",\"phone\"] 未匹配检索规则。</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= items.length <= 10<sup>4</sup></code></li>\n",
    "\t<li><code>1 <= type<sub>i</sub>.length, color<sub>i</sub>.length, name<sub>i</sub>.length, ruleValue.length <= 10</code></li>\n",
    "\t<li><code>ruleKey</code> 等于 <code>\"type\"</code>、<code>\"color\"</code> 或 <code>\"name\"</code></li>\n",
    "\t<li>所有字符串仅由小写字母组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-items-matching-a-rule](https://leetcode.cn/problems/count-items-matching-a-rule/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-items-matching-a-rule](https://leetcode.cn/problems/count-items-matching-a-rule/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[\"phone\",\"blue\",\"pixel\"],[\"computer\",\"silver\",\"lenovo\"],[\"phone\",\"gold\",\"iphone\"]]\\n\"color\"\\n\"silver\"', '[[\"phone\",\"blue\",\"pixel\"],[\"computer\",\"silver\",\"phone\"],[\"phone\",\"gold\",\"iphone\"]]\\n\"type\"\\n\"phone\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        if ruleKey == 'type':\n",
    "            return sum(1 for item in items if item[0] == ruleValue)\n",
    "        elif ruleKey == 'color':\n",
    "            return sum(1 for item in items if item[1] == ruleValue)\n",
    "        else:\n",
    "            return sum(1 for item in items if item[2] == ruleValue)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        mp = dict(type=0, color=1, name=2)\n",
    "        ret = 0\n",
    "        for x in items:\n",
    "            if ruleValue == x[mp[ruleKey]]:\n",
    "                ret += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        map = {'type': 0, 'color': 1, 'name': 2}\n",
    "        idx = map[ruleKey]\n",
    "        ans = 0\n",
    "        for item in items:\n",
    "            if item[idx] == ruleValue:\n",
    "                ans += 1\n",
    "            else:\n",
    "                pass\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        hash = {\"type\": 0, \"color\": 1, \"name\": 2}\n",
    "        i = hash[ruleKey]\n",
    "        \n",
    "        nums = 0\n",
    "        for item in items:\n",
    "            if item[i] == ruleValue:\n",
    "                nums += 1\n",
    "        \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        count=0\n",
    "        for item in items:\n",
    "            if ruleKey=='type' and item[0]==ruleValue:\n",
    "                count=count+1\n",
    "            elif ruleKey=='color' and item[1]==ruleValue:\n",
    "                count=count+1\n",
    "            elif ruleKey=='name' and item[2]==ruleValue:\n",
    "                count=count+1\n",
    "            \n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        dic = {\"type\": 0, \"color\": 1, \"name\": 2}\n",
    "        return sum(item[dic[ruleKey]] == ruleValue for item in items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        sum_product = 0\n",
    "        if ruleKey == 'color':\n",
    "            key = 1\n",
    "        if ruleKey ==  'type':\n",
    "            key = 0\n",
    "        if ruleKey == 'name':\n",
    "            key = 2\n",
    "        for i in range(len(items)):\n",
    "            if items[i][key] == ruleValue:\n",
    "                sum_product = sum_product + 1\n",
    "        return sum_product"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        ans=0\n",
    "        if ruleKey==\"type\":\n",
    "            for item in items:\n",
    "                if item[0]==ruleValue:\n",
    "                    ans+=1\n",
    "        if ruleKey==\"color\":\n",
    "            for item in items:\n",
    "                if item[1]==ruleValue:\n",
    "                    ans+=1\n",
    "        if ruleKey==\"name\":\n",
    "            for item in items:\n",
    "                if item[2]==ruleValue:\n",
    "                    ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        ans = 0\n",
    "        for t, c, n in items:\n",
    "            if ruleKey == 'type':\n",
    "                ans += ruleValue == t\n",
    "            if ruleKey == 'color':\n",
    "                ans += ruleValue == c\n",
    "            if ruleKey == 'name':\n",
    "                ans += ruleValue == n \n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        ret = 0\n",
    "        for i in range(0, len(items)):\n",
    "            if ruleKey == \"type\" and  ruleValue == items[i][0]:\n",
    "                ret +=1\n",
    "            elif ruleKey == \"color\" and  ruleValue == items[i][1]:\n",
    "                ret +=1\n",
    "            elif ruleKey == \"name\" and  ruleValue == items[i][2]:\n",
    "                ret +=1\n",
    "        return ret\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        m = {'type':0,'color':1,'name':2}\n",
    "        return sum(ruleValue==i[m[ruleKey]] for i in items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        i = 0 if ruleKey[0] == 't' else (1 if ruleKey[0] == 'c' else 2)\n",
    "        return sum(v[i] == ruleValue for v in items)\n",
    "\n",
    "\n",
    "          \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        x=0\n",
    "        for i in items:\n",
    "            if ruleKey == 'type'and i[0] == ruleValue or ruleKey == \"color\"and i[1] == ruleValue or ruleKey == \"name\"and i[2] == ruleValue:\n",
    "                x+=1\n",
    "        return x\n",
    "          \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(items)):\n",
    "            if ruleKey =='type' and items[i][0] == ruleValue:\n",
    "                count = count + 1\n",
    "            elif ruleKey =='color' and items[i][1] == ruleValue:\n",
    "                count = count + 1\n",
    "            elif ruleKey =='name' and items[i][2] == ruleValue:\n",
    "                count = count + 1\n",
    "            else:\n",
    "                count = count\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        count = 0\n",
    "        if ruleKey == \"type\":\n",
    "            for i in items:\n",
    "                if i[0] == ruleValue:\n",
    "                    count += 1\n",
    "        elif ruleKey == \"color\":\n",
    "            for i in items:\n",
    "                if i[1] == ruleValue:\n",
    "                    count += 1\n",
    "        elif ruleKey == \"name\":\n",
    "            for i in items:\n",
    "                if i[2] == ruleValue:\n",
    "                    count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        count = 0\n",
    "        for x in items:\n",
    "            if ruleKey == \"type\":\n",
    "                if x[0] == ruleValue:\n",
    "                    count += 1\n",
    "            elif ruleKey == \"color\":\n",
    "                if x[1] == ruleValue:\n",
    "                    count += 1\n",
    "            else:\n",
    "                if x[2] == ruleValue:\n",
    "                    count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        acc=0\n",
    "        for item in items:\n",
    "            if ruleKey ==\"type\":\n",
    "                if item[0]==ruleValue:\n",
    "                    acc+=1\n",
    "                    continue\n",
    "            if ruleKey ==\"color\":\n",
    "                if item[1]==ruleValue:\n",
    "                    acc+=1\n",
    "                    continue\n",
    "            if ruleKey ==\"name\":\n",
    "                if item[2]==ruleValue:\n",
    "                    acc+=1   \n",
    "                    continue             \n",
    "        return acc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        return sum(a[{'type':0,'color':1,'name':2}[ruleKey]]==ruleValue for a in items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        dic = {\"type\": 0, \"color\": 1, \"name\": 2,}\n",
    "        return sum(item[dic[ruleKey]] == ruleValue for item in items)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        dic = {\"type\":0, \"color\":1, \"name\":2}\n",
    "        res = 0\n",
    "        key_index = dic.get(ruleKey)\n",
    "        for j in range(len(items)):\t\t\n",
    "\t           if items[j][key_index] == ruleValue:\n",
    "                    res += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        res = 0\n",
    "        for item in items:\n",
    "            if ruleKey == 'type':\n",
    "                if item[0] == ruleValue:\n",
    "                    res += 1\n",
    "            if ruleKey == 'color':\n",
    "                if item[1] == ruleValue:\n",
    "                    res += 1\n",
    "            if ruleKey == 'name':\n",
    "                if item[2] == ruleValue:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        index = {\"type\": 0, \"color\": 1, \"name\": 2}[ruleKey]\n",
    "        return sum(item[index] == ruleValue for item in items)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        key_index = 0\n",
    "        for i, key in enumerate([\"type\", \"color\", \"name\"]):\n",
    "            if key == ruleKey:\n",
    "                key_index = i\n",
    "                break\n",
    "        count = 0\n",
    "        for item in items:\n",
    "            if item[key_index] == ruleValue:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        cnt = 0 \n",
    "        index = 0\n",
    "        if ruleKey==\"color\":\n",
    "            index = 1\n",
    "        elif ruleKey==\"name\":\n",
    "            index = 2\n",
    "        for i in items:\n",
    "            if i[index] == ruleValue:\n",
    "                cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        n = len(items)\n",
    "        num = 0\n",
    "        for i, item in enumerate(items):\n",
    "            if ruleKey == \"type\":\n",
    "                if item[0] == ruleValue:\n",
    "                    num = num + 1\n",
    "            if ruleKey == \"color\":\n",
    "                if item[1] == ruleValue:\n",
    "                    num = num + 1\n",
    "            if ruleKey == \"name\":\n",
    "                if item[2] == ruleValue:\n",
    "                    num = num + 1\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        if ruleKey == 'type':\n",
    "            key = 0\n",
    "        elif ruleKey == 'color':\n",
    "            key = 1\n",
    "        elif ruleKey == 'name':\n",
    "            key = 2\n",
    "        num = 0\n",
    "        for i in items:\n",
    "            if i[key] == ruleValue:\n",
    "                num += 1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        cnt = 0 \n",
    "        index = 0\n",
    "        if ruleKey==\"color\":\n",
    "            index = 1\n",
    "        elif ruleKey==\"name\":\n",
    "            index = 2\n",
    "        for i in items:\n",
    "            if i[index] == ruleValue:\n",
    "                cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        dic = {\"type\": 0,\"color\": 1,\"name\": 2}\n",
    "        num = 0\n",
    "        for i in range(len(items)):\n",
    "            if items[i][dic[ruleKey]] == ruleValue:\n",
    "                num += 1\n",
    "            else:\n",
    "                continue\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, t: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        n=0\n",
    "        for items in t:\n",
    "            if ruleKey == \"type\" and ruleValue == items[0]:\n",
    "                n+=1\n",
    "            elif ruleKey == \"color\" and ruleValue == items[1]:\n",
    "                n+=1\n",
    "            elif ruleKey == \"name\" and ruleValue == items[2]:\n",
    "                n+=1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        x=0\n",
    "        ans=0\n",
    "        if ruleKey==\"color\":\n",
    "            x=1\n",
    "        elif ruleKey==\"name\":\n",
    "            x=2\n",
    "        for l in items:\n",
    "            if l[x]==ruleValue:\n",
    "                ans+=1\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        steps = 0\n",
    "        for infor in items:\n",
    "            if ruleKey == 'type' and infor[0] == ruleValue:\n",
    "                steps += 1\n",
    "            elif ruleKey == 'color' and infor[1] == ruleValue:\n",
    "                steps += 1\n",
    "            elif ruleKey == 'name' and infor[2] == ruleValue:\n",
    "                steps += 1\n",
    "        return steps\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        s=0 \n",
    "        sum=0\n",
    "        if ruleKey == \"type\":\n",
    "            s=0\n",
    "        if ruleKey == \"color\":\n",
    "            s=1\n",
    "        if ruleKey == \"name\":\n",
    "            s=2\n",
    "        for i in items:\n",
    "            if i[s]==ruleValue:\n",
    "                sum=sum+1\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        m = ['type','color','name']\n",
    "        idx = m.index(ruleKey)\n",
    "        return sum(ruleValue==i[idx] for i in items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        n = 0\n",
    "        if ruleKey == 'type':\n",
    "            for i in items:\n",
    "                if i[0] == ruleValue:\n",
    "                    n += 1          \n",
    "        if ruleKey == 'color':\n",
    "            for i in items:\n",
    "                if i[1] == ruleValue:\n",
    "                    n += 1          \n",
    "        if ruleKey == 'name':\n",
    "            for i in items:\n",
    "                if i[2] == ruleValue:\n",
    "                    n += 1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        index = {\"type\":0,\"color\":1,\"name\":2}[ruleKey]\n",
    "        return sum(item[index] == ruleValue for item in items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        ans = 0\n",
    "        for v in items:\n",
    "            if ruleKey == \"type\" and ruleValue == v[0]:\n",
    "                ans += 1\n",
    "            elif ruleKey == \"color\" and ruleValue == v[1]:\n",
    "                ans += 1\n",
    "            elif ruleKey == \"name\" and ruleValue == v[2]:\n",
    "                ans += 1\n",
    "        return ans\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        # index = {\n",
    "        #     \"type\":0,\n",
    "        #     \"color\":1,\n",
    "        #     \"name\":2\n",
    "        # }[ruleKey]\n",
    "        # ans = 0\n",
    "        # for item in items:\n",
    "        #     if item[index] == ruleValue:\n",
    "        #         ans += 1\n",
    "        # return ans\n",
    "\n",
    "        ans, index = 0, 0 if ruleKey[0] == \"t\" else 1 if ruleKey[0] == \"c\" else 2\n",
    "\n",
    "        for item in items:\n",
    "            if item[index] == ruleValue:\n",
    "                ans += 1\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        ans = 0\n",
    "        if ruleKey == \"type\":\n",
    "            key = 0\n",
    "        elif ruleKey == \"color\":\n",
    "            key = 1\n",
    "        else:\n",
    "            ruleKey == \"name\"\n",
    "            key = 2\n",
    "        for i in items:\n",
    "            if i[key] == ruleValue:\n",
    "                ans += 1\n",
    "        return ans\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        d={\"type\":0,\"color\":1,\"name\":2}\n",
    "        return len(list(filter(lambda item:item[d[ruleKey]]==ruleValue,items)))\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        dic = {\"type\" : 0, \"color\" : 1, \"name\" : 2}\n",
    "        ans = 0\n",
    "        for x in items:\n",
    "            if x[dic[ruleKey]] == ruleValue:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        itemIndex = {\"type\":0, \"color\": 1, \"name\": 2}[ruleKey]\n",
    "\n",
    "\n",
    "\n",
    "        res = 0\n",
    "\n",
    "        for i in range(len(items)):\n",
    "            if items[i][itemIndex] == ruleValue:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        dic = {\"type\": 0, \"color\": 1, \"name\": 2,}\n",
    "        return sum(item[dic[ruleKey]] == ruleValue for item in items)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        result = 0\n",
    "        for item in items:\n",
    "            if ruleKey == 'type':\n",
    "                if item[0] == ruleValue:\n",
    "                    result += 1\n",
    "            elif ruleKey == 'color':\n",
    "                if item[1] == ruleValue:\n",
    "                    result += 1\n",
    "            elif ruleKey == 'name':\n",
    "                if item[2] == ruleValue:\n",
    "                    result += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        count:int = 0 \n",
    "        lst = ['type', 'color', 'name']\n",
    "        k = lst.index(ruleKey)\n",
    "        for i in range(len(items)):\n",
    "            if k == 0 and ruleValue == items[i][0]:\n",
    "                count += 1\n",
    "                continue\n",
    "            elif k == 1 and ruleValue == items[i][1]:\n",
    "               count += 1\n",
    "               continue\n",
    "            elif k == 2 and ruleValue == items[i][2]:\n",
    "                count += 1\n",
    "                continue\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        index={\"type\":0,\"color\":1,\"name\":2}[ruleKey]\n",
    "        return sum(item[index]==ruleValue for item in items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        a=0\n",
    "        if ruleKey=='type':\n",
    "            for i in items:\n",
    "                if i[0]==ruleValue:\n",
    "                    a+=1\n",
    "        elif ruleKey=='color':\n",
    "            for i in items:\n",
    "                if i[1]==ruleValue:\n",
    "                    a+=1\n",
    "        else:\n",
    "            for i in items:\n",
    "                if i[2]==ruleValue:\n",
    "                    a+=1\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        count = 0\n",
    "        d = {\n",
    "            \"type\":0,\n",
    "            \"color\":1,\n",
    "            \"name\":2\n",
    "        }\n",
    "        index = d[ruleKey]\n",
    "        for item in items:\n",
    "            if item[index] == ruleValue:\n",
    "                count += 1\n",
    "        \n",
    "        return count\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        a=0\n",
    "        for i in range(len(items)):\n",
    "            if ruleKey==\"type\" and ruleValue==items[i][0]:\n",
    "                a+=1\n",
    "            if ruleKey==\"color\" and ruleValue==items[i][1]:\n",
    "                a+=1\n",
    "            if ruleKey==\"name\" and ruleValue==items[i][2]:\n",
    "                a+=1\n",
    "        return a\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        d = {\"type\": 0, \"color\": 1, \"name\": 2}\n",
    "        return sum([1 if item[d[ruleKey]] == ruleValue else 0 for item in items])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        matchCount = 0\n",
    "        for item in items:\n",
    "            print(item)\n",
    "            if ruleKey == \"type\" and ruleValue == item[0]:\n",
    "               matchCount += 1\n",
    "            if ruleKey == \"color\" and ruleValue == item[1]:\n",
    "                matchCount += 1\n",
    "            if ruleKey == \"name\" and ruleValue == item[2]:\n",
    "                matchCount +=1\n",
    "        return matchCount\n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        ans=0\n",
    "        for  item in items:\n",
    "            if ruleKey=='type':\n",
    "                if item[0]==ruleValue:\n",
    "                    ans+=1\n",
    "            if ruleKey=='color':\n",
    "                if item[1]==ruleValue:\n",
    "                    ans+=1\n",
    "            if ruleKey=='name':\n",
    "                if item[2]==ruleValue:\n",
    "                    ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        d = {\n",
    "            'type': 0,\n",
    "            'color': 1,\n",
    "            'name': 2\n",
    "        }\n",
    "        count = 0\n",
    "        for item in items:\n",
    "            if item[d[ruleKey]] == ruleValue:\n",
    "                count += 1\n",
    "        \n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        ruleKey_dict={'type':0,'color':1,'name':2}\n",
    "        count=0\n",
    "        for item in items:\n",
    "            if item[ruleKey_dict[ruleKey]]==ruleValue:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        index = {\"type\": 0, \"color\": 1, \"name\": 2}[ruleKey]\n",
    "        return sum(item[index] == ruleValue for item in items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        count = 0\n",
    "        for item in items:\n",
    "            if ruleKey == \"type\" and item[0] == ruleValue:\n",
    "                count += 1\n",
    "            elif ruleKey == \"color\" and item[1] == ruleValue:\n",
    "                count += 1\n",
    "            elif ruleKey == \"name\" and item[2] == ruleValue:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        if ruleKey=='type':\n",
    "            return sum(1 for i in items if i[0]==ruleValue)\n",
    "        elif ruleKey=='color':\n",
    "            return sum(1 for i in items if i[1]==ruleValue)\n",
    "        else:\n",
    "            return sum(1 for i in items if i[2]==ruleValue)\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        a=0\n",
    "        for i in range(len(items)):\n",
    "            if ruleKey==\"type\" and ruleValue==items[i][0]:\n",
    "                a+=1\n",
    "            if ruleKey==\"color\" and ruleValue==items[i][1]:\n",
    "                a+=1\n",
    "            if ruleKey==\"name\" and ruleValue==items[i][2]:\n",
    "                a+=1\n",
    "        return a\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        count = 0\n",
    "        key = {\"type\": 0, \"color\": 1, \"name\": 2}\n",
    "        for item in items:\n",
    "            if item[key[ruleKey]] == ruleValue:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        dic = {\"type\": 0,\"color\": 1,\"name\": 2}\n",
    "        num = 0\n",
    "        for i in range(len(items)):\n",
    "            if items[i][dic[ruleKey]] == ruleValue:\n",
    "                num += 1\n",
    "            else:\n",
    "                continue\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        temp = {'type':0 , 'color':1,'name':2}\n",
    "        ans = 0\n",
    "        for item in items:\n",
    "            if item[temp[ruleKey]] == ruleValue:\n",
    "                ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def count_matches(items: list[list[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "    idx = None\n",
    "    match ruleKey:\n",
    "        case \"type\":\n",
    "            idx = 0\n",
    "        case \"color\":\n",
    "            idx = 1\n",
    "        case \"name\":\n",
    "            idx = 2\n",
    "    res = 0\n",
    "    for item in items:\n",
    "        if item[idx] == ruleValue:\n",
    "            res += 1\n",
    "    return res\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        return count_matches(items, ruleKey, ruleValue)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:\n",
    "        dic={'type':0,'color':1,'name':2}\n",
    "        count=0\n",
    "        for item in items:\n",
    "            if item[dic[ruleKey]]==ruleValue:\n",
    "             count+=1\n",
    "        return count"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
